package xj.graph2d.shapes;

import static xj.util.xml.XMLUtil.getXMLAttributeString;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JTextField;

import xj.graph2d.DrawShape;
import xj.graph2d.RectangleShape;
import xj.lang.CodeWriter;

public class ComponentShape extends RectangleShape {

  public ComponentShape() {
    needFrameRect = false;
  }

  public ComponentShape(JComponent comp) {
    this.comp = comp;
    needFrameRect = false;
    if (comp != null) {
      Dimension d = comp.getPreferredSize();
      setEnds(0, 0, d.width, d.height);
    }
  }

  @Override
  public DrawShape makeInstance() {
    return new ComponentShape();
  }

  @Override
  public Object clone() {
    ComponentShape s = null;
    if (comp != null) {
      s = new ComponentShape(cloneComponent(comp));
    } else {
      s = new ComponentShape();
    }
    return s;
  }

  @Override
  public String getName() {
    return "Component";
  }

  @Override
  public void draw(Graphics2D g2) {
    float x = (float) Math.min(p1.getX(), p2.getX());
    float y = (float) Math.min(p1.getY(), p2.getY());
    float w = (float) Math.abs(p1.getX() - p2.getX());
    float h = (float) Math.abs(p1.getY() - p2.getY());

    if (comp != null) {
      // painter.drawComponent(comp, x, y, w, h);
      comp.setSize((int) w, (int) h);
      Graphics cg = g2.create((int) x, (int) y, (int) w, (int) h);
      try {
	comp.paint(cg);
      } finally {
	cg.dispose();
      }
    } else {
      g2.draw(new Rectangle2D.Float(x, y, w, h));
    }

  }

  public JComponent getComponent() {
    return comp;
  }

  public void setComponent(JComponent comp) {
    this.comp = comp;
  }

  @Override
  protected void writeXMLElements(CodeWriter out, int mode,
      String namespacePrefix) {
    if (out != null) {
      super.writeXMLElements(out, mode, namespacePrefix);

      if (comp != null) {
	String nodename = getPrefixedName("Component", namespacePrefix);
	out.println("<" + nodename + ' '
	    + getXMLAttributeString("class", comp.getClass().getName()) + "/>");
      }
    }
  }

  @Override
  protected void processXMLElement(org.w3c.dom.Element e, String namespaceURI) {
    if (e != null) {
      String nodename = e.getLocalName();
      if ("Component".equals(nodename)) {
	String classname = e.getAttribute("class");
	try {
	  Class compclass = Class.forName(classname);
	  comp = (JComponent) compclass.newInstance();
	} catch (ClassNotFoundException ex) {
	} catch (InstantiationException ex) {
	} catch (IllegalAccessException ex) {
	}
      } else {
	super.processXMLElement(e, namespaceURI);
      }
    }
  }

  /*
         * public void writeXML(CodeWriter out, int mode, String
         * namespacePrefix) { if (out != null) { String nodename =
         * getPrefixedName("ComponentShape", namespacePrefix); out.indentInc();
         * out.println("<" + nodename + ">"); if (comp != null) { out.println("<Component
         * class=\"" + comp.getClass().getName() + "\"/>"); }
         * out.thisIndentDec(); out.println("</" + nodename + ">"); } }
         */

  protected JComponent cloneComponent(JComponent comp) {
    if (comp != null) {
      if (comp instanceof JButton) {
	JButton button = (JButton) comp;
	JButton newbutton = new JButton(button.getText());
	return newbutton;
      } else if (comp instanceof JTextField) {
	JTextField tf = (JTextField) comp;
	JTextField newtf = new JTextField(30);
	newtf.setText(tf.getText());
	return newtf;
      } else {
	return null;
      }
    } else {
      return null;
    }
  }

  protected JComponent comp;

}
